
/**
 * 
 * FrameMain.java
 * @author xmarti
 * @Funcionament.
 *  en funció de l'usuari ens caldrà carregar una cosa  o un altra.
 *  si es un administrador global nomes ens cal carregar els usuaris, empreses. 
 *  en el cas d'un administrador local nomes ens cal carregar l'empresa en questió. amb les entrades, sortides i 
 * Created on 02/08/2012, 20:53:08
 */
package screens.start;
import commons.UtilsLanguage;
import javax.swing.JOptionPane;
import screens.user.FrameUser;
import server.RemoteImplCLLAMB;

/**
 *
 * @author xmarti
 */
public class FrameMain extends javax.swing.JFrame {
    
    private RemoteImplCLLAMB remot=null;
    private DialogStart dStart=null;
    /** Creates new form FrameMain */
    
    public FrameMain() {
        
        initComponents();         
        this.setLocationRelativeTo(null);
        remot= new RemoteImplCLLAMB();         
        checkUser();
    }
   private void checkUser(){    
       if (dStart==null){
           dStart= new DialogStart(this,true,remot);
       }       
       dStart.setVisible(true);       
       //JOptionPane.showMessageDialog(null, dStart.getUser().getName(),"Missatge",JOptionPane.OK_OPTION);
       if(dStart.getUser()!=null){
            Login();
            if(dStart.getUser().getLevel()==0){
                //administrador Global
                //nomes pot administrar usuaris
                jMenuItemUser.setEnabled(true);
                jMenuCompany.setVisible(false);
            }else if (dStart.getUser().getLevel()==1){
                //administrador Companyia
                //nomes pot administrar la seva companyia
                jMenuItemUser.setEnabled(true);
            }else if (dStart.getUser().getLevel()==2){
                //usuari normal
                //no pot fer canvis de companyia
                jMenuItemUser.setEnabled(false);
            }
            
       }else{
            Logout();
       }
           
           
   }
   private void Login(){
       jLabelUser.setText(dStart.getUser().toString());
       jMenuItemLogin.setEnabled(false);
       jMenuItemLogout.setEnabled(true);       
   }
   private void Logout(){
       jLabelUser.setText("Cap usuari Connectat");
       jMenuItemLogin.setEnabled(true);
       jMenuItemLogout.setEnabled(false);
   }    
    
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabelUser = new javax.swing.JLabel();
        jMenuBar = new javax.swing.JMenuBar();
        jMenuUser = new javax.swing.JMenu();
        jMenuItemLogin = new javax.swing.JMenuItem();
        jMenuItemLogout = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jMenuItemUser = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenuCompany = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(UtilsLanguage.getMessage("FrameMain.Caption"));

        jLabel1.setText(UtilsLanguage.getMessage("FrameMain.User"));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelUser, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabelUser, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(58, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        jMenuUser.setText(UtilsLanguage.getMessage("FrameMain.MenuUser"));

        jMenuItemLogin.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemLogin.setText(UtilsLanguage.getMessage("FrameMain.ItemLogin"));
        jMenuItemLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemLoginActionPerformed(evt);
            }
        });
        jMenuUser.add(jMenuItemLogin);

        jMenuItemLogout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemLogout.setText(UtilsLanguage.getMessage("FrameMain.ItemLogout"));
        jMenuItemLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemLogoutActionPerformed(evt);
            }
        });
        jMenuUser.add(jMenuItemLogout);
        jMenuUser.add(jSeparator2);

        jMenuItemUser.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemUser.setText(UtilsLanguage.getMessage("FrameMain.ItemUser"));
        jMenuItemUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemUserActionPerformed(evt);
            }
        });
        jMenuUser.add(jMenuItemUser);
        jMenuUser.add(jSeparator1);

        jMenuItemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemExit.setText(UtilsLanguage.getMessage("FrameMain.ItemExit"));
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });
        jMenuUser.add(jMenuItemExit);

        jMenuBar.add(jMenuUser);

        jMenuCompany.setText("Empreses");

        jMenuItem1.setText("Gestió Empreses");
        jMenuCompany.add(jMenuItem1);

        jMenuBar.add(jMenuCompany);

        setJMenuBar(jMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(225, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLogoutActionPerformed
       if (dStart!=null){dStart.setUser(null);}
       Logout();
       
    }//GEN-LAST:event_jMenuItemLogoutActionPerformed

    private void jMenuItemLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLoginActionPerformed
        checkUser();
    }//GEN-LAST:event_jMenuItemLoginActionPerformed

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        if(JOptionPane.showConfirmDialog(null,UtilsLanguage.getMessage("OptionPane.Exit"), UtilsLanguage.getMessage("OptionPane.Confirm"), JOptionPane.OK_CANCEL_OPTION)==0){
            this.dispose();
            System.exit(0);
        }
    }//GEN-LAST:event_jMenuItemExitActionPerformed

    private void jMenuItemUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemUserActionPerformed
        // TODO add your handling code here:
        FrameUser fUser= new FrameUser(0);  
        fUser.setLocationRelativeTo(null);
        fUser.setVisible(true);
        
    }//GEN-LAST:event_jMenuItemUserActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                UtilsLanguage.setDefaultLenguage("i18n/messages");
                new FrameMain().setVisible(true);
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelUser;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenu jMenuCompany;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemLogin;
    private javax.swing.JMenuItem jMenuItemLogout;
    private javax.swing.JMenuItem jMenuItemUser;
    private javax.swing.JMenu jMenuUser;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    // End of variables declaration//GEN-END:variables
}
